package com.programmercarl.hash;

import java.util.*;

/**
 * https://leetcode.cn/problems/group-anagrams/description/
 *
 * @author yulongtian
 * @create 2024-12-13 21:41
 */
public class Test02 {
    public static void main(String[] args) {

    }

    public static List<List<String>> groupAnagrams(String[] strs) {
        String[] count = new String[strs.length];
        for (int i = 0; i < strs.length; i++) {
            int[] temp_count = new int[26];
            for (int j = 0; j < strs[i].length(); j++) {
                temp_count[strs[i].charAt(j) - 'a']++;
            }
            count[i] = Arrays.toString(temp_count);
        }
        List<List<String>> result = new ArrayList<>();
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            if (map.containsKey(count[i])) {
                map.get(count[i]).add(strs[i]);
            } else {
                List<String> temp = new ArrayList<>();
                temp.add(strs[i]);
                map.put(count[i], temp);
            }
        }
        for (String s : map.keySet()) {
            result.add(map.get(s));
        }
        return result;
    }

}
